﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Data.SQLite;
namespace Clipper.DAL
{
    /// <summary>
    /// 数据访问基础类(基于SQLite)
    /// </summary>
    public abstract class SQLiteHelper
    {
        //数据库连接字符串(app.config来配置)，可以动态更改connectionString支持多数据库.
        static string mFilename = AppDomain.CurrentDomain.BaseDirectory + @"DATA\Clipper.db";
        
        //static string filename = @"..\..\DATA\Clipper.db";
        public static string connectionString = "Data Source="+mFilename+";Version=3";//";Password="
        public SQLiteHelper()
        {
            
        }


        #region 公用方法

        /// <summary>
        /// 获得当前表的最大值
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static int GetMaxID(string FieldName, string TableName)
        {
            string strsql = "select max(" + FieldName + ") from " + TableName;
            object obj = GetSingle(strsql);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
        /// <summary>
        /// 获得当前最大值加1
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static int GetNewMaxID(string FieldName, string TableName)
        {
            string strsql = "select max(" + FieldName + ")+1 from " + TableName;
            object obj = GetSingle(strsql);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }

        public static bool Exists(string strSql)
        {
            object obj = GetSingle(strSql);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
        {
            object obj = GetSingle(strSql, cmdParms);
            int cmdresult;
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion

        #region  执行简单SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.SQLite.SQLiteException E)
                    {
                        connection.Close();
                        throw new Exception(E.Message);
                    }
                }
            }
        }

        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">多条SQL语句</param>		
        public static void ExecuteSqlTran(ArrayList SQLStringList)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connectionString))
            {
                conn.Open();
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.Connection = conn;
                SQLiteTransaction tx = conn.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n].ToString();
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            cmd.ExecuteNonQuery();
                        }
                    }
                    tx.Commit();
                }
                catch (System.Data.SQLite.SQLiteException E)
                {
                    tx.Rollback();
                    throw new Exception(E.Message);
                }
            }
        }
        /// <summary>
        /// 执行带一个存储过程参数的的SQL语句。
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <param name="content">参数内容,比如一个字段是格式复杂的文章，有特殊符号，可以通过这个方式添加</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, string content)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
                SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
                myParameter.Value = content;
                cmd.Parameters.Add(myParameter);
                try
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (System.Data.SQLite.SQLiteException E)
                {
                    throw new Exception(E.Message);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }
            }
        }
        /// <summary>
        /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
                SQLiteParameter myParameter = new SQLiteParameter("@fs", DbType.Binary);
                myParameter.Value = fs;
                cmd.Parameters.Add(myParameter);
                try
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
                catch (System.Data.SQLite.SQLiteException E)
                {
                    throw new Exception(E.Message);
                }
                finally
                {
                    cmd.Dispose();
                    connection.Close();
                }
            }
        }

        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static object GetSingle(string SQLString)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        object obj = cmd.ExecuteScalar();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (System.Data.SQLite.SQLiteException e)
                    {
                        connection.Close();
                        throw new Exception(e.Message);
                    }
                }
            }
        }
        /// <summary>
        /// 执行查询语句，返回SQLiteDataReader
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>SQLiteDataReader</returns>
        public static SQLiteDataReader ExecuteReader(string strSQL)
        {
            SQLiteConnection connection = new SQLiteConnection(connectionString);
            SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
            try
            {
                connection.Open();
                SQLiteDataReader myReader = cmd.ExecuteReader();
                return myReader;
            }
            catch (System.Data.SQLite.SQLiteException e)
            {
                throw new Exception(e.Message);
            }

        }
        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SQLite.SQLiteException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }


        #endregion

        #region 执行带参数的SQL语句

        /// <summary>
        /// 执行SQL语句，返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        return rows;
                    }
                    catch (System.Data.SQLite.SQLiteException E)
                    {
                        ; throw new Exception(E.Message);
                    }
                }
            }
        }


        /// <summary>
        /// 执行多条SQL语句，实现数据库事务。
        /// </summary>
        /// <param name="SQLStringList">SQL语句的哈希表（key为sql语句，value是该语句的SQLiteParameter[]）</param>
        public static void ExecuteSqlTran(Hashtable SQLStringList)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connectionString))
            {
                conn.Open();
                using (SQLiteTransaction trans = conn.BeginTransaction())
                {
                    SQLiteCommand cmd = new SQLiteCommand();
                    try
                    {
                        //循环
                        foreach (DictionaryEntry myDE in SQLStringList)
                        {
                            string cmdText = myDE.Key.ToString();
                            SQLiteParameter[] cmdParms = (SQLiteParameter[])myDE.Value;
                            PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
                            int val = cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();

                            trans.Commit();
                        }
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }
        }


        /// <summary>
        /// 执行一条计算查询结果语句，返回查询结果（object）。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果（object）</returns>
        public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                using (SQLiteCommand cmd = new SQLiteCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (System.Data.SQLite.SQLiteException e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }
        }

        /// <summary>
        /// 执行查询语句，返回SQLiteDataReader
        /// </summary>
        /// <param name="strSQL">查询语句</param>
        /// <returns>SQLiteDataReader</returns>
        public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms)
        {
            SQLiteConnection connection = new SQLiteConnection(connectionString);
            SQLiteCommand cmd = new SQLiteCommand();
            try
            {
                PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                SQLiteDataReader myReader = cmd.ExecuteReader();
                cmd.Parameters.Clear();
                return myReader;
            }
            catch (System.Data.SQLite.SQLiteException e)
            {
                throw new Exception(e.Message);
            }

        }

        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                SQLiteCommand cmd = new SQLiteCommand();
                PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        da.Fill(ds, "ds");
                        cmd.Parameters.Clear();
                    }
                    catch (System.Data.SQLite.SQLiteException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    return ds;
                }
            }
        }


        private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, SQLiteParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
                cmd.Transaction = trans;
            cmd.CommandType = CommandType.Text;//cmdType;
            if (cmdParms != null)
            {
                foreach (SQLiteParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }

        #endregion

        #region 导出导入数据
        /// <summary>
        /// 创建加密数据库
        /// </summary>
        /// <param name="fileName">文件</param>
        /// <param name="password">密码</param>
        /// <returns>结果</returns>
        public static bool CreateDatabase(string fileName,string password)
        {
            SQLiteConnection.CreateFile(fileName);//创建
            SQLiteConnection cnn2 = new SQLiteConnection(@"Data Source=" + fileName);
            cnn2.Open();
            cnn2.ChangePassword(password);
            cnn2.Close();
            return true;
        }
        public static bool OpenDatabase(string fileName, string password)
        {
            bool res=false;
            try
            {
                SQLiteConnection cnn2 = new SQLiteConnection(@"Data Source=" + fileName);
                cnn2.SetPassword(password);
                cnn2.Open();
                SQLiteCommand cmd = new SQLiteCommand(cnn2);
                cmd.CommandText = "select * from sqlite_master";
                cmd.ExecuteNonQuery();
                res=true;
            }
            catch (Exception ex)
            {
                res = false;
                throw ex;
            }
            return res;
        }
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="dataTable">数据集，DataTable名称需要是表名</param>
        /// <param name="filename">导出文件的路径</param>
        /// <param name="password">密码</param>
        public static void ExportData(DataTable dataTable, string fileName, string password)
        {
            SQLiteConnection cnn2 = new SQLiteConnection(@"Data Source=" + fileName);
            cnn2.SetPassword(password);
            cnn2.Open();
            SQLiteTransaction ts = cnn2.BeginTransaction();
            string strCreate = "create table " + dataTable.TableName + "(";
            string strInsert = "insert or replace into " + dataTable.TableName + "(";

            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                DataColumn column = dataTable.Columns[i];
                if (i < dataTable.Columns.Count - 1)
                {
                    strCreate += column.ColumnName + ",";
                    strInsert += column.ColumnName + ",";
                }
                else
                {
                    strCreate += column.ColumnName + ")";
                    strInsert += column.ColumnName + ")";
                }
            }
            SQLiteCommand cteate = new SQLiteCommand(strCreate, cnn2);
            cteate.ExecuteNonQuery();

            foreach (DataRow row in dataTable.Rows)
            {
                string strInsertValue = " values (";
                for (int i = 0; i < row.ItemArray.Length; i++)
                {
                    DataColumn column = dataTable.Columns[i];
                    if (column.DataType == typeof(string))
                    {
                        if (i < dataTable.Columns.Count - 1)
                        {
                            strInsertValue += "'" + row[i] + "',";
                        }
                        else
                        {

                            if (row[i].ToString() != "")
                            {
                                strInsertValue += "'" + row[i] + "')";
                            }
                            else
                            {
                                strInsertValue += "null)";
                            }
                        }
                    }
                    else
                    {
                        if (i < dataTable.Columns.Count - 1)
                        {
                            strInsertValue += row[i] + ",";
                        }
                        else
                        {

                            if (row[i].ToString() != "")
                            {
                                strInsertValue += row[i] + ")";
                            }
                            else
                            {
                                strInsertValue += "null)";
                            }
                        }
                    }
                }
                string cmd = strInsert + strInsertValue;
                SQLiteCommand insert = new SQLiteCommand(cmd, cnn2);
                insert.ExecuteNonQuery();
            }
            ts.Commit();
            cnn2.Close();
        }


        /// <summary>
        /// 导入数据到已有表中
        /// </summary>
        /// <param name="dataTable">数据集，DataTable名称需要是表名</param>
        /// <param name="filename">数据库文件</param>
        /// <param name="password">数据库密码</param>
        public static  void ImportData(string filename,string tableName,string password)
        {
                //连接数外部数据库
                System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection();
                System.Data.SQLite.SQLiteConnectionStringBuilder connstr
                    = new System.Data.SQLite.SQLiteConnectionStringBuilder();
                connstr.DataSource = filename;
                connstr.Password = password;//设置密码
                con.ConnectionString = connstr.ToString();
                con.Open();

                //查询外部数据库
                string sql = " select * from " + tableName;
                SQLiteDataAdapter dta = new SQLiteDataAdapter(sql, con);
                SQLiteCommandBuilder scb = new SQLiteCommandBuilder(dta);
                dta.InsertCommand = scb.GetInsertCommand();
                DataSet DS = new DataSet();
                dta.FillSchema(DS, SchemaType.Source, tableName);//加载表架构
                dta.Fill(DS, tableName);    //加载表数据
                DataTable dataTable = DS.Tables[tableName];
                con.Close();
            
            //将数据导入到本数据库中
            using (SQLiteConnection conLocation = new SQLiteConnection(connectionString))
            {
                conLocation.Open();
                SQLiteTransaction ts = conLocation.BeginTransaction();
                string strCreate = "create table " + tableName + "(";
                string strInsert = "insert or replace into " + tableName + "(";

                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    DataColumn column = dataTable.Columns[i];
                    if (i < dataTable.Columns.Count - 1)
                    {
                        strCreate += column.ColumnName + ",";
                        strInsert += column.ColumnName + ",";
                    }
                    else
                    {
                        strCreate += column.ColumnName + ")";
                        strInsert += column.ColumnName + ")";
                    }
                }
                //不需要创建表
                //SQLiteCommand cteate = new SQLiteCommand(strCreate, conLocation);
                //cteate.ExecuteNonQuery();

                foreach (DataRow row in dataTable.Rows)
                {
                    string strInsertValue = " values (";
                    for (int i = 0; i < row.ItemArray.Length; i++)
                    {
                        DataColumn column = dataTable.Columns[i];
                        if (column.DataType == typeof(string))
                        {
                            if (i < dataTable.Columns.Count - 1)
                            {
                                strInsertValue += "'" + row[i] + "',";
                            }
                            else
                            {

                                if (row[i].ToString() != "")
                                {
                                    strInsertValue += "'" + row[i] + "')";
                                }
                                else
                                {
                                    strInsertValue += "null)";
                                }
                            }
                        }
                        else
                        {
                            if (i < dataTable.Columns.Count - 1)
                            {
                                //strInsertValue += row[i] + ",";
                                strInsertValue += "'" + row[i] + "',";
                            }
                            else
                            {
                                if (row[i].ToString() != "")
                                {
                                    strInsertValue += row[i] + ")";
                                }
                                else
                                {
                                    strInsertValue += "null)";
                                }
                            }
                        }
                    }
                    string cmdInsert = strInsert + strInsertValue;
                    SQLiteCommand insert = new SQLiteCommand(cmdInsert, conLocation);
                    insert.ExecuteNonQuery();
                }
                ts.Commit();
            }

        }
        
        /// <summary>
        /// 导入数据到多个已有表
        /// </summary>
        /// <param name="ds">DatSet</param>
        public static bool ImportData(DataSet ds)
        {
            bool res = false;
            using (SQLiteConnection conLocation = new SQLiteConnection(connectionString))
            { 
                conLocation.Open();
                    //将数据导入到本数据库中
                SQLiteTransaction ts = conLocation.BeginTransaction();
                foreach (DataTable dataTable in ds.Tables)
                {
                    string strCreate = "create table " + dataTable.TableName + "(";
                    string strInsert = "insert or replace into " + dataTable.TableName + "(";
                    for (int i = 0; i < dataTable.Columns.Count; i++)
                    {
                        DataColumn column = dataTable.Columns[i];
                        if (i < dataTable.Columns.Count - 1)
                        {
                            strCreate += column.ColumnName + ",";
                            strInsert += column.ColumnName + ",";
                        }
                        else
                        {
                            strCreate += column.ColumnName + ")";
                            strInsert += column.ColumnName + ")";
                        }
                    }
                    foreach (DataRow row in dataTable.Rows)
                    {
                        string strInsertValue = " values (";
                        for (int i = 0; i < row.ItemArray.Length; i++)
                        {
                            DataColumn column = dataTable.Columns[i];
                            if (column.DataType == typeof(string))
                            {
                                if (i < dataTable.Columns.Count - 1)
                                {
                                    strInsertValue += "'" + row[i] + "',";
                                }
                                else
                                {

                                    if (row[i].ToString() != "")
                                    {
                                        strInsertValue += "'" + row[i] + "')";
                                    }
                                    else
                                    {
                                        strInsertValue += "null)";
                                    }
                                }
                            }
                            else
                            {
                                if (i < dataTable.Columns.Count - 1)
                                {
                                    //strInsertValue += row[i] + ",";
                                    strInsertValue += "'" + row[i] + "',";
                                }
                                else
                                {
                                    if (row[i].ToString() != "")
                                    {
                                        strInsertValue += row[i] + ")";
                                    }
                                    else
                                    {
                                        strInsertValue += "null)";
                                    }
                                }
                            }
                        }
                        string cmdInsert = strInsert + strInsertValue;
                        SQLiteCommand insert = new SQLiteCommand(cmdInsert, conLocation);
                        insert.ExecuteNonQuery();
                    }
                }

                ts.Commit();
                res = true;
            }
            return res;
        }

        /// <summary>
        /// 更新有数据的列，无数据的不更改
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool ImportUpdateData(DataTable dataTable)
        {
            bool res = false;
            using (SQLiteConnection conLocation = new SQLiteConnection(connectionString))
            {
                conLocation.Open();
                //将数据导入到本数据库中
                SQLiteTransaction ts = conLocation.BeginTransaction();
                string strUpdate = "update " + dataTable.TableName +" set ";
                string strInsert = "insert or replace into " + dataTable.TableName + "(";
                int columnCount = dataTable.Columns.Count;
                object[] args = new object[columnCount+1];//参数 
                string primaryKey="";//先认为第一列为主键
                for (int i = 0; i < columnCount; i++)
                {
                    DataColumn column = dataTable.Columns[i];
                    if (i == 0)
                    {
                        primaryKey = column.ColumnName;
                    }
                    if (i < dataTable.Columns.Count - 1)
                    {
                        strUpdate += column.ColumnName + "='{"+i+"}',";
                        strInsert += column.ColumnName + ",";
                    }
                    else
                    {
                        strUpdate += column.ColumnName + "='{" + i + "}' where "+primaryKey+"='{"+(i+1)+"}'";
                        strInsert += column.ColumnName + ")";
                    }
                }
                foreach (DataRow row in dataTable.Rows)
                {
                    string strInsertValue = " values (";
                    for (int i = 0; i < row.ItemArray.Length; i++)
                    {
                        DataColumn column = dataTable.Columns[i];

                        if (i < dataTable.Columns.Count - 1)
                        {
                            args[i] = row[i];
                            strInsertValue += "'" + row[i] + "',";
                        }
                        else
                        {
                            if (row[i].ToString() != "")
                            {
                                args[i] = row[i];
                                args[i + 1] = row[0];
                                strInsertValue += "'" + row[i] + "')";
                            }
                            else
                            {
                                args[i] = null;
                                strInsertValue += "null)";
                            }
                        }
                    }
                    string strCmdUpdate = string.Format(strUpdate, args);
                    SQLiteCommand cmdUpdate = new SQLiteCommand(strCmdUpdate, conLocation);
                    if (cmdUpdate.ExecuteNonQuery() <= 0)//没有更新则添加
                    {
                        string cmdInsert = strInsert + strInsertValue;
                        SQLiteCommand insert = new SQLiteCommand(cmdInsert, conLocation);
                        insert.ExecuteNonQuery();
                    }
                }
                ts.Commit();
                res = true;
            }
            return res;
        }
        #endregion 
    }
}
